package com.je21121.controller;

import com.je21121.model.UserInfo;
import com.je21121.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/LoginController")
public class LoginController extends HttpServlet {
    UserService userService = new UserService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 判断 参数  调用对应的方法
        String method = request.getParameter("method");
        System.out.println(method);
        if("login".equals(method)){
            login(request,response);//登录
        }else if("register".equals(method)){
            addUser(request,response);// 注册
        }
    }

    // 路径映射
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserInfo userInfo = new UserInfo(username,password,1);
        boolean flag = userService.userlogin(userInfo);
        if(flag){
            // 登录成功  跳转教务系统管理界面
            HttpSession session = request.getSession();
            //  把登录成功的userinfo 存到session中
            session.setAttribute("userInfo",userInfo);
            //  设置有效期10秒
            //session.setMaxInactiveInterval(30);
            request.getRequestDispatcher("/index.html").forward(request,response);
        }else{
            // 登录失败  跳转回登录界面
            response.sendRedirect("login.html");
        }
    }

    private void addUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserInfo userInfo = new UserInfo(username,password,2);
        System.out.println(username);
        boolean flag = checkUserParam(userInfo);
        if(flag){
            //  调用jdbc 添加用户
            flag = userService.addUser(userInfo);
            if(flag){
                System.out.println("注册成功");
            }else{
                System.out.println("注册失败");
            }
        }else{

        }
        response.sendRedirect("login.html");
    }

    private boolean checkUserParam(UserInfo userInfo){
        if(userInfo.getUsername() == null || userInfo.getUsername().equals("")){
            System.out.println("用户名不能为空");
            return false;
        }
        if(userInfo.getPassword() == null || userInfo.getPassword().equals("")){
            System.out.println("密码不能为空");
            return false;
        }
        return true;
    }
}
